﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Windows.Input;
using ViewMaker.Core.Controls;
using System.Windows.Markup;

namespace ViewMaker.Core.ViewModels
{
    [View(ViewControlType.Grid, Title = "XamlGenerator Template")]
    [ViewProperty(GridViewControl.Properties.Margin, "10")]
#if !SILVERLIGHT
    [ViewProperty(GridViewControl.Properties.WindowWidth, 600)]
    [ViewProperty(GridViewControl.Properties.WindowHeight, 400)]
#else
        [ViewProperty(GridViewControl.Properties.Width, 600)]
        [ViewProperty(GridViewControl.Properties.Height, 400)]
#endif
    public class TemplateEditViewModel : ViewModelBase
    {
        private WindowSettings settings;

        [ViewProperty(TextBoxViewControl.Properties.HeaderPosition, LayoutHeaderPosition.Hidden)]
        [ViewProperty(TextBoxViewControl.Properties.IsMultiline, true)]
        [ViewProperty(TextBoxViewControl.Properties.GridHeight, "*")]
        [ViewProperty(TextBoxViewControl.Properties.Height, null)]
        [ViewProperty(TextBoxViewControl.Properties.HorizontalAlignment, LayoutHorizontalAlignment.Stretch)]
        public string Template
        {
            get { return _template; }
            set { _template = value; OnPropertyChanged("Template"); }
        }
        private string _template;

        [Display(Name = "OK", Order = 1)]
        public ICommand OK
        {
            get
            {
                return CreateCommand(() =>
                {
                    try
                    {
#if !SILVERLIGHT
                        XamlReader.Parse(Template);
#else
                            XamlReader.Load(Template);
#endif
                        settings.Template = Template;
                    }
                    catch (Exception ex)
                    {
                        this.ShowMessage(ex.Message, "Error");
                    }
                    ExecuteViewCommand(ViewCommands.Close);
                });
            }
        }

        [Display(Name = "Reset", Order = 2)]
        public ICommand Reset
        {
            get
            {
                return CreateCommand(() =>
                {
#if !SILVERLIGHT
                    Template = ViewMaker.Core.Wpf.WpfXamlGenerator.Template;
#else
                        Template = ViewMaker.Core.Silverlight.SlXamlGenerator.Template;
#endif
                    settings.Template = null;
                });
            }
        }

        [Display(Name = "Cancel", Order = 3)]
        public ICommand Cancel { get { return CreateViewCommand(ViewCommands.Close); } }

        public TemplateEditViewModel(WindowSettings settings)
        {
            this.settings = settings;
            this.Template = settings.Template;
            if (string.IsNullOrWhiteSpace(Template))
            {
#if !SILVERLIGHT
                Template = ViewMaker.Core.Wpf.WpfXamlGenerator.Template;
#else
                    Template = ViewMaker.Core.Silverlight.SlXamlGenerator.Template;
#endif
            }
        }
    }

}
